<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>宿舍管理系统</title>
    <meta name="renderer" content="webkit">
    <meta name="csrf-token" content="{{ csrf_token() }}" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
        content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="{{ asset('css/font.css') }}">
    <link rel="stylesheet" href="{{ asset('css/xadmin.css') }}">
    <script src="{{ asset('lib/layui/layui.js') }}" charset="utf-8"></script>
    <script type="text/javascript" src="{{ asset('js/xadmin.js') }}"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
    <div class="layui-fluid">
        <div class="layui-row">
            <form action="" method="post" class="layui-form layui-form-pane">
                {{ csrf_field() }}
                <div class="layui-form-item">
                    <label for="name" class="layui-form-label">
                        <span class="x-red">*</span>用户名
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="name" name="name" required="" lay-verify="required"
                            value="{{ $user->name }}" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label" for="company_id">所属公司</label>
                    <div class="layui-input-block">
                        <select name="company_id">
                            <option value="" {{ $user->company_id ? '' : 'selected' }}>无</option>
                            @foreach ($companies as $k => $v)
                                <option value="{{ $k }}" {{ $user->company_id == $k ? 'selected' : '' }}>
                                    {{ $v }}</option>
                            @endforeach
                        </select>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="email" class="layui-form-label">
                        <span class="x-red">*</span>邮箱
                    </label>
                    <div class="layui-input-inline">
                        <input type="text" id="email" name="email" required="" lay-verify="required|email"
                            value="{{ $user->email }}" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="email" class="layui-form-label">
                        不修改密码
                    </label>
                    <div class="layui-input-inline">
                        <input type="radio" name="dont_modify_pwd" value="1" title="是" checked
                            lay-filter="modify_pwd">
                        <input type="radio" name="dont_modify_pwd" value="0" title="否"
                            lay-filter="modify_pwd">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_pass" class="layui-form-label">
                        密码
                    </label>
                    <div class="layui-input-inline">
                        <input type="password" id="L_pass" name="password" required="" disabled autocomplete="off"
                            class="layui-input">
                    </div>
                    <div class="layui-form-mid layui-word-aux">
                        6到16个字符
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="L_repass" class="layui-form-label">
                        确认密码
                    </label>
                    <div class="layui-input-inline">
                        <input type="password" id="L_repass" name="repass" required="" disabled autocomplete="off"
                            class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="is_super" class="layui-form-label">超级管理员</label>
                    <div class="layui-input-block">
                        <input type="radio" name="is_super" value="1" title="是"
                            {{ $user->is_super ? 'checked' : '' }}>
                        <input type="radio" name="is_super" value="0" title="否"
                            {{ $user->is_super ? '' : 'checked' }}>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label for="active" class="layui-form-label">激活</label>
                    <div class="layui-input-block">
                        <input type="radio" name="active" value="1" title="是"
                            {{ $user->active ? 'checked' : '' }}>
                        <input type="radio" name="active" value="0" title="否"
                            {{ $user->active ? '' : 'checked' }}>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">
                        角色
                    </label>
                    <div class="layui-input-block">
                        @foreach ($roles as $k => $v)
                            <input type="checkbox" name="role_id[]" value="{{ $k }}" lay-skin="primary"
                                title="{{ $v }}" {{ in_array($k, $role_ids) ? 'checked' : '' }}>
                        @endforeach
                    </div>
                </div>
                <div class="layui-form-item">
                    <button class="layui-btn" lay-submit="" lay-filter="save">保存</button>
                </div>
            </form>
        </div>
    </div>
    <script>
        layui.use(['form', 'layer'], function() {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;

            //自定义验证规则
            form.verify({
                pass: [/(.+){6,12}$/, '密码必须6到12位'],
                repass: function(value) {
                    if ($('#L_pass').val() != $('#L_repass').val()) {
                        return '两次密码不一致';
                    }
                }
            });

            //监听提交
            form.on('submit(save)',
                function(data) {
                    console.log(data.field);
                    //发异步删除数据
                    $.ajaxSetup({
                        headers: {
                            'X-CSRF-Token': $('input[name="_token"]').val()
                        }
                    });
                    let url = '/users/{{ $user->id }}';
                    $.ajax({
                        url: url,
                        data: data.field,
                        type: 'PATCH',
                        dataType: "json",
                        statusCode: {
                            401: function() {
                                console.log("statusCode 401 called");
                                window.location.reload();
                            },
                            449: function() {
                                console.log("statusCode 449 called");
                            }
                        },
                        success: function(d) {
                            if (d.error) {
                                layer.msg('保存失败:' + d.message, {
                                    icon: 2,
                                    time: 3000
                                });
                            } else {
                                layer.alert('保存成功', {
                                    icon: 6,
                                    time: 5000
                                }, function() {
                                    //关闭当前frame
                                    xadmin.close();
                                    // 可以对父窗口数据表进行刷新
                                    parent.layui.table.reload('test');
                                });
                            }
                        },
                        error: function(err) {
                            console.log(err);
                            layer.msg('保存失败', {
                                icon: 2,
                                time: 1000
                            });
                        }
                    });
                    //ajax 异步调用会发生再这个语句之后，返回false可以避免异步返回时页面发生了刷新
                    return false;
                });

            form.on('checkbox(father)', function(data) {
                if (data.elem.checked) {
                    $(data.elem).parent().siblings('td').find('input').prop("checked", true);
                    form.render();
                } else {
                    $(data.elem).parent().siblings('td').find('input').prop("checked", false);
                    form.render();
                }
            });

            form.on('radio(modify_pwd)', function(d) {
                if (d.value === '1') {
                    console.log('on');
                    $('#L_pass').attr('disabled', true);
                    $('#L_pass').attr('lay-verify', '');
                    $('#L_repass').attr('disabled', true);
                    $('#L_repass').attr('lay-verify', '');
                } else {
                    console.log('off')
                    $('#L_pass').attr('disabled', false);
                    $('#L_pass').attr('lay-verify', 'pass');
                    $('#L_repass').attr('disabled', false);
                    $('#L_repass').attr('lay-verify', 'repass');
                }
            });
        });
    </script>
</body>

</html>
